package com.lbs.servlet;

import com.lbs.entity.Stu;
import com.lbs.service.Impl.StuServiceImpl;
import com.lbs.service.StuService;
import com.lbs.until.BaseServletUntil;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.http.Part;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * @author Administrator
 * @date 2021/5/18 14:31
 * @description
 **/
@MultipartConfig
public class StuServlet extends BaseServletUntil {

    public void addStu(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name = request.getParameter("name");
        String gender = request.getParameter("gender");
        String bir = request.getParameter("bir");
        String hobby = Arrays.toString(request.getParameterValues("hobby"));
        Part part = request.getPart("file");
        List<String> suffix = Arrays.asList(".jpg", ".jpeg", ".png", ".bmp", ".gif");
        long picSize = 2 * 1024 * 1024;
        String fileName = part.getSubmittedFileName();
        long size = part.getSize();

        Stu stu = new Stu();
        stu.setName(name);
        stu.setGender(gender);
        stu.setBir(bir);
        stu.setHobby(hobby);


        if (!fileName.equals("")) {
            String fileSuffix = fileName.substring(fileName.lastIndexOf("."));
            fileName = System.currentTimeMillis() + fileSuffix;
            if (suffix.contains(fileSuffix)) {
                if (size <= picSize) {
                    part.write("C:\\Users\\说好的幸福呢\\Desktop\\upload\\" + fileName);
                    stu.setPhoto(fileName);
                }
            }
        }

        StuService stuService = new StuServiceImpl();
        boolean flag = stuService.addStu(stu);
        if (flag) {
            System.out.println("添加成功！");
            response.sendRedirect("queryStu.do");
        } else {
            System.out.println("注册失败！");
            response.sendRedirect("addStu.jsp");
        }
    }

    public void queryStu(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String name = request.getParameter("name");

        String gender = request.getParameter("gender");

        StuService stuService = new StuServiceImpl();

        List<Stu> stuList = stuService.searchStu(name, gender);

        HttpSession session = request.getSession();

        session.setAttribute("stuList", stuList);

        response.sendRedirect("index.jsp");
    }

    // http://localhost:8080/stuSystem_war_exploded/bachDelStu.do?ids=1,2,3
    public void bachDelStu(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String ids = request.getParameter("ids");
        //System.out.println(ids);

        StuService stuService = new StuServiceImpl();

        boolean flag = stuService.delStu(ids);

        if (flag){
            //System.out.println("删除成功");
            response.getWriter().println("<script>alert('删除成功！')</script>");
            response.sendRedirect("queryStu.do");
        }else {
            //System.out.println("删除失败");
            response.getWriter().println("<script>alert('删除失败！')</script>");
            response.sendRedirect("index.jsp");
        }
    }

    public void updateStuById(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{
        String id = request.getParameter("id");
        StuService stuService = new StuServiceImpl();
        Stu stu = stuService.selectStuById(id);
        request.setAttribute("stu",stu);
        request.getRequestDispatcher("updateStu.jsp").forward(request, response);
    }

    public void updateStu(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        // 获取表单的所有数据
        String id = request.getParameter("id");
        String name = request.getParameter("name");
        String gender = request.getParameter("gender");
        String bir = request.getParameter("bir");
        String hobby = Arrays.toString(request.getParameterValues("hobby"));
        String oldPhoto = request.getParameter("oldPhoto");

        Part part = request.getPart("file");
        List<String> suffix = Arrays.asList(".jpg", ".jpeg", ".png", ".bmp", ".gif");
        long picSize = 2 * 1024 * 1024;
        String fileName = part.getSubmittedFileName();
        long size = part.getSize();

        Stu stu = new Stu();
        stu.setId(Integer.valueOf(id));
        stu.setName(name);
        stu.setGender(gender);
        stu.setBir(bir);
        stu.setHobby(hobby);
        stu.setPhoto(oldPhoto);

        if (!fileName.equals("")){
            String fileSuffix = fileName.substring(fileName.lastIndexOf("."));
            fileName = System.currentTimeMillis() + fileSuffix;
            if (suffix.contains(fileSuffix)) {
                if (size <= picSize) {
                    part.write("C:\\Users\\说好的幸福呢\\Desktop\\upload\\" + fileName);
                    stu.setPhoto(fileName);
                }
            }
        }

        StuService stuService = new StuServiceImpl();


        boolean flag = stuService.updateStu(stu);

        if (flag){
            System.out.println("修改成功！");
            response.sendRedirect("queryStu.do");
        }else {
            System.out.println("修改失败！");
            response.sendRedirect("updateStu.jsp");
        }


    }

}
